package com.un.ebs.businessObject.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.un.ebs.core.enums.DataTypeEnum;
import com.un.ebs.core.metadata.FunctionInfo;
import com.un.ebs.core.metadata.MetadataField;
import com.un.ebs.core.metadata.RequestArg;
import com.un.ebs.sys.domain.Column;
import com.un.ebs.sys.domain.Table;
import com.un.ebs.sys.enums.FunctionsEnums;
import com.un.ebs.sys.mapper.ColumnMapper;
import com.un.ebs.businessObject.service.RelUpdateService;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author zc
 * @ClassName RelUpdateServiceImpl
 * @Description
 * @Date 2021/1/12
 */
@Slf4j
@Service("RelUpdateServiceImpl")
public class RelUpdateServiceImpl implements RelUpdateService {
	@Autowired
	private ColumnMapper columnMapper;

	@Override
	public FunctionInfo getUpdateFunc(Table table) {
		FunctionInfo func = new FunctionInfo();
		String url = FunctionsEnums.UPDATE.getUrl();
		String desc = FunctionsEnums.UPDATE.getDesc();
		List<RequestMethod> method = Arrays.asList(FunctionsEnums.UPDATE.getRequestMethod());

		func.setUrl(String.format(url, table.getId()));
		func.setDescription(desc);
		func.setMethod(method);
		func.setName(table.getTableName());

		LambdaQueryWrapper<Column> qw = new LambdaQueryWrapper<Column>();
		qw.eq(Column::getTableId, table.getId());
		List<Column> columns = columnMapper.selectList(qw);

		List<RequestArg> requestArgs = new ArrayList<>();

		List<MetadataField> metadataFields = new ArrayList<>();
		for (Column column : columns) {
			RequestArg requestArg = new RequestArg();
			requestArg.setName(column.getColumnName());
			requestArg.setDisplayName(column.getColumnDesc());
			requestArg.setDataType(column.getColumnType());
			requestArg.setIsArray(0);
			requestArg.setNull(column.getIsNullable());
			requestArg.setLength(column.getLength());
			requestArgs.add(requestArg);

		}
		List<RequestArg> request = getListRequest(requestArgs);
		List<MetadataField> response = getListResponse(metadataFields);
		func.setRequest(request);
		func.setResponse(response);
		return func;
	}

	private List<MetadataField> getListResponse(List<MetadataField> metadataFields) {
		if (CollectionUtil.isEmpty(metadataFields)) {
			MetadataField metadataField = new MetadataField();
			metadataField.setName("row_count");
			metadataField.setIsArray(0);
			metadataField.setDataType(DataTypeEnum.INT);
			metadataFields.add(metadataField);
		}
		List<MetadataField> list = new ArrayList<>();
		// 1.构造code
		MetadataField m1 = new MetadataField();
		m1.setName("code");
		m1.setIsArray(0);
		m1.setDataType(DataTypeEnum.INT);
		// 2.构造message
		MetadataField m2 = new MetadataField();
		m2.setName("message");
		m2.setIsArray(0);
		m2.setDataType(DataTypeEnum.STRING);
		// 3.构造data
		MetadataField m3 = new MetadataField();
		m3.setName("data");
		m3.setIsArray(0);
		m3.setDataType(DataTypeEnum.OBJECT);
		m3.setChildField(metadataFields);
		list.add(m1);
		list.add(m2);
		list.add(m3);
		return list;
	}

	private List<RequestArg> getListRequest(List<RequestArg> requestArgs) {
		return requestArgs;
	}
}
